package com.lusoft.onepiece.manager;

import java.util.ArrayList;
import java.util.List;

import org.andengine.entity.IEntity;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.ui.activity.BaseGameActivity;

import android.util.Log;
import android.widget.Toast;

import com.lusoft.onepiece.constant.GameConstant;
import com.lusoft.onepiece.monster.MonsterRegion;
import com.lusoft.onepiece.util.GameUtil;

public class MonsterManager {

	private static final MonsterManager monsterManager = new MonsterManager();
	
	private List<MonsterRegion> unused = new ArrayList<MonsterRegion>();
	
	private List<MonsterRegion> used = new ArrayList<MonsterRegion>();
	
	private AnimatedSprite hero;
	
	private MonsterManager(){
		
	}
	
	public static MonsterManager getInstance(){
		return monsterManager;
	}
	
	public void init(BaseGameActivity activity, AnimatedSprite hero){
		for(int i=0;i<GameConstant.Monster.INIT_SIZE;i++) {
			MonsterRegion monsterRegion = new MonsterRegion(activity, GameUtil.randomX(), 100, 128, 128, "monster.png");
			unused.add(monsterRegion);
		}
		this.hero = hero;
	}
	
	public void show(IEntity entity){
		for(int i=unused.size()-1;i>-1;i--) {
			MonsterRegion region = unused.get(i);
			entity.attachChild(region.getMonster());
			used.add(region);
			unused.remove(i);
		}
	}
	
	public void dealAttack(){
		for(int i=used.size()-1;i>-1;i--) {
			MonsterRegion region = used.get(i);
			if(region.getMonster().collidesWith(hero)) {
//				Toast.makeText(activity, "Attack!", Toast.LENGTH_SHORT).show();
				Log.d("TIP", "attack");
				used.remove(i);
				unused.add(region);
			}
		}
	}
	
}
